#!/usr/bin/node

const http = require('http'),
      fs = require('fs'),
      path = require('path'),
      pic = process.argv[2];

try{
  if(process.argv.length !== 3){
    console.error('命令行参数格式：cmd fileName');
    process.exit(1);
  }

  let data = fs.readFileSync(pic).toString('base64');
  let ext = path.extname(pic);
  let uriData = 'data:image/' + ext.slice(1,ext.length) + ';base64,' + data;

  //log(`uri:${uri}\n`);

  let html = '<html><head><title>data uri</title></head><body><img alt="'+path.basename(pic,ext)+'" src ="'+uriData+'"/></body></html>';

  http.createServer((req,res)=>{
    res.end(html);
  }).listen(8080);
}catch(e){
  console.error('something wrong!');
}
